#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

  struct ListNode {
     int val;
     struct ListNode *next;
 };

void Backinsert(struct ListNode** phead, int val)
{
    assert(phead);
    struct ListNode* newnode = malloc(sizeof(struct ListNode));
    if (newnode==NULL)
    {
        perror("malloc");
    }
    newnode->val = val;
    newnode->next = NULL;

    if (*phead == NULL)
    {
        *phead = newnode;
        return;
    }
    struct ListNode* pr = *phead;
    while (pr->next)
    {
        pr = pr->next;
    }
    pr->next = newnode;
    return;
}

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {

    if (list1 == NULL && list2 == NULL)
        return NULL;
    if (list1 == NULL)
        return list2;
    if (list2 == NULL)
        return list1;

    struct ListNode* newhead = NULL;

    while (list1 != NULL && list2 != NULL)
    {
        if (list1->val <= list2->val)
        {
            Backinsert(&newhead, list1->val);
            list1 = list1->next;
        }
        else
        {
            Backinsert(&newhead, list2->val);
            list2 = list2->next;
        }
        return newnode;
    }


    int main()
    {




        return 0;
    }
    


